# -*- mode: shell-script -*-

PATH="./build:${PATH}"

# MacOS specific checks
if [ "$(uname)" == "Darwin" ] ; then

  # We use temporary directories during testing that need to be
  # accessible to the Docker daemon. By default on Macs, $TMPDIR
  # (which mktemp responds to) is set to a user-specific location that
  # the Docker daemon cannot read from.
  #
  # In some environments, such as TeamCity, $TMPDIR is intentionally
  # pointed elsewhere, so we only want to override the default
  # value. (We don't currently run these builds on Macs, but you never
  # know.) This default seems to be in /private/var/folders on some
  # Macs and /var/folders on others, so we accommodate both.
  if [[ "${TMPDIR}" == */var/folders* ]]; then
    export TMPDIR=/tmp
  fi

  echo "Setting PATH with MacOS specific locations"
  export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
  export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
  export PATH="/usr/local/opt/make/libexec/gnubin:$PATH"

  if ! grep --version 2>/dev/null | grep -q "GNU grep" ; then
    cat >&2 <<EOF
== GNU grep is required. Fix with ==

  brew install grep --with-default-names

EOF
  fi

  if ! sed --version 2>/dev/null | grep -q "GNU sed" ; then
    cat >&2 <<EOF
== GNU sed is required. Fix with ==

  brew install gnu-sed

EOF
  fi

  if ! readlink --version 2>/dev/null | grep -q "GNU coreutils" ; then
    cat >&2 <<EOF
== GNU coreutils is required. Fix with ==

  brew install coreutils  --with-default-names

EOF
  fi

  # have to use || here because BSD find doesnt even support --version (it errors)
  # shellcheck disable=SC2185
  find --version 1>/dev/null 2>/dev/null || cat >&2 <<EOF
== GNU findutils is required. Fix with ==

    brew install findutils  --with-default-names

EOF
fi # MacOS specific checks

if ! which make >/dev/null; then
    if which apt-get >/dev/null; then
        sudo apt-get install make
    else
        echo "Please install GNU Make 4.0 or later"
    fi
fi

if [[ -f devenv.local ]]; then
    source devenv.local
    # to be consistent with the rest of neo4j we should use NEO4JVERSION exclusively but unfortunately both with and without underscore are used in this repo
    export NEO4JVERSION
    NEO4J_VERSION="${NEO4JVERSION}"
    export NEO4J_VERSION
else
    echo >&2 "Error: cannot find devenv.local"
fi
